/* MOS                                                        */
/* Giordano Mion, Luca David Opromolla and Alessandro Sforza  */
/* University of Sussex, Banco de Portugal, and LSE           */

program drop _all
clear all
clear matrix
capture log close
set more off

local path = "/Users/lucadavidopromolla/Projects/MOS_rev"
cd "`path'/computation"
log using "`path'/logs/MOS_trade_performance_common_dataset_Apr_29_2022_rev.log", replace

set mem 12g

//////////////////////////////////////////////////////////////////
/////////////////        Destination         /////////////////////
//////////////////////////////////////////////////////////////////


use firm_regressions_exp, clear
keep empresa ano v_est_ dest exp exp_ rexp* ln_firmage foreign sh_skilled sh_skilled_d nace2 n2emp pemp vendas_euro meanwk_age meanwk_educ mean_age_exp mean_educ_exp de_ sdwk_age sdwk_educ sd_age_exp sd_educ_exp  new_exporter cont_exporter exit_exporter never_exporter other_exporter exporting npc_alt mant*
keep if ano>=1995 & ano<=2005
rename exp_ mkt_spec_exp
rename rexp_ r3mkt_spec_exp
forvalues i=1/2 {
rename mante`i'_ mkt_spec_exp`i'
}



// Attach variables "number of products exported per market group" and "avg exports per product, within a market group"
sort npc_alt ano dest
merge 1:1 npc_alt ano dest using product2.dta
drop if _merge==2
drop _merge

// Attach mean and sd of worker fe from wage regression
sort empresa ano

// Attach mean and sd of worker fe from wage regression by manager type
sort empresa ano

/*
// Attach PT exports (Exports of Portugal to the rest of the World, by nace 2-digits and year in thousands of US dollars, 1995-2005 from BACI)
sort nace2 ano
merge nace2 ano using "`path'/data/other/Export_PT_95_05_Nace2.dta"
*/
drop if nace2==37

/* Create some regressors */
gen relsize = log(pemp)
gen relapp_lbprod = log(vendas_euro/pemp)
rename meanwk_age relmean_age  /* Note that this is computed considering managers only, see mob_exports file */
rename sdwk_age relsd_age
rename meanwk_educ relmean_educ
rename sdwk_educ relsd_educ
replace relsd_age = 0 if relsd_age==. & relmean_age!=.
replace relsd_educ = 0 if relsd_educ==. & relmean_educ!=.
gen ln_exports = log(v_est_)
// Create instrument
egen firm_dest = group(empresa dest)
xtset firm_dest ano
gen inst = l3.mkt_spec_exp
gen instr3 = l3.r3mkt_spec_exp
/* Some renaming */
foreach z of varlist mean_age_exp sd_age_exp mean_educ_exp sd_educ_exp  {
rename `z' rel`z' 
}
// Labelling
label var exp "Manag. w/ Exp. (0/1)"
label var relsize "Firm Size (log)"
label var relapp_lbprod "App. Labor Productivity (log)"
label var ln_firmage "Firm Age (log)"
label var foreign "Foreign Ownership (0/1)"
label var relmean_age "Avg. Managers' Age"
label var relsd_age "Std. Dev. Managers' Age"
label var relmean_educ "Avg. Managers' Education" 
label var relsd_educ "Std. Dev. Managers' Education"
***label var PT_exports "Exports PT"
label var sh_skilled "Share of Skilled Workers"
label var mkt_spec_exp "Manag. w/ Spec. Exp. (0/1)"
label var empresa "firms"
label var mant1 "Other manager (0/1)"
label var mant2 "Sales manager (0/1)"
label var mante1 "OM w/ Export Exp. (0/1)"
label var mante2 "SM w/ Export Exp. (0/1)"

// Fixed Effects
egen dest_time = group(dest ano)
tab dest_time,gen(dt_dummy)
egen firm_time = group(empresa ano)
label var firm_time "firm-year pairs"

save MMT_summarystats_tradeb,replace
save MMT_trade_statusb,replace
save MMT_summarystats_trade_intb,replace
save MMT_trade_status_intb,replace

// Locals
local instruct "tex(pretty frag) tdec(3) rdec(3) auto(3) bdec(3) sdec(3) symbol($^a$,$^b$,$^c$) se label e(N) nocons"
local ctr_exp_mf = "relsize relapp_lbprod ln_firmage foreign sh_skilled relmean_age relsd_age relmean_educ relsd_educ"
local ctr_exp_mf_mt = "relsize relapp_lbprod ln_firmage foreign sh_skilled relmean_age relsd_age relmean_educ relsd_educ"


// Regressions sample  
marksample entry_mod2
markout `entry_mod2' de_ dest other_exporter exp rexp* mkt_spec_exp r3mkt_spec_exp `ctr_exp_mf' dt_dummy* empresa dest_time mante1 mante2  // Deflating variables
keep if `entry_mod2'==1
sort empresa ano dest
save MMT_trade_status_entryb_s,replace
save MMT_trade_status_entry_intb_s,replace
keep empresa ano
gen var1 = 1
collapse var1, by(empresa ano) 
save dataset1, replace


//////////////////////////////////////////////////////////////////
//////////////////        Product         ////////////////////////
//////////////////////////////////////////////////////////////////


use firm_regressions_exp_gr, clear
keep empresa ano v_est_ pr_gr pr_gr_ exp exp_ rexp* ln_firmage foreign sh_skilled sh_skilled_d nace2 n2emp pemp vendas_euro meanwk_age meanwk_educ mean_age_exp mean_educ_exp sdwk_age sdwk_educ sd_age_exp sd_educ_exp  new_exporter cont_exporter exit_exporter never_exporter other_exporter exporting npc_alt mant*
keep if ano>=1995 & ano<=2005
rename exp_ prgr_spec_exp
rename rexp_ r3prgr_spec_exp
forvalues i=1/2 {
rename mante`i'_ prgr_spec_exp`i'
}



// Attach variables "number of markets the product is exported to" and "avg exports per market, within a product group"
sort npc_alt ano pr_gr
merge 1:1 npc_alt ano pr_gr using product3.dta
drop if _merge==2
drop _merge

// Attach mean and sd of worker fe from wage regression
sort empresa ano
             
// Attach mean and sd of worker fe from wage regression by manager type
sort empresa ano

// Attach indices of product differentiation and external financial dependence
sort pr_gr
merge m:1 pr_gr using rauch.dta
keep if _merge==3
drop _merge

/* Create some regressors */
gen relsize = log(pemp)
gen relapp_lbprod = log(vendas_euro/pemp)
rename meanwk_age relmean_age  /* Note that this is computed considering managers only, see mob_exports file */
rename sdwk_age relsd_age
rename meanwk_educ relmean_educ
rename sdwk_educ relsd_educ
replace relsd_age = 0 if relsd_age==. & relmean_age!=.
replace relsd_educ = 0 if relsd_educ==. & relmean_educ!=.
gen ln_exports = log(v_est_)
// Create instrument
egen firm_prgr = group(empresa pr_gr)
xtset firm_prgr ano
gen inst = l3.prgr_spec_exp
gen instr3 = l3.r3prgr_spec_exp
gen double r_prgr_spec_exp = prgr_spec_exp*rauch_w
gen double r_r3prgr_spec_exp = r3prgr_spec_exp*rauch_w
gen double r_inst = inst*rauch_w
gen double r_instr3 = instr3*rauch_w


/* Some renaming */
foreach z of varlist mean_age_exp sd_age_exp mean_educ_exp sd_educ_exp  {
rename `z' rel`z' 
}
// Labelling
label var exp "Manag. w/ Exp. (0/1)"
label var relsize "Firm Size (log)"
label var relapp_lbprod "App. Labor Productivity (log)"
label var ln_firmage "Firm Age (log)"
label var foreign "Foreign Ownership (0/1)"
label var relmean_age "Avg. Managers' Age"
label var relsd_age "Std. Dev. Managers' Age"
label var relmean_educ "Avg. Managers' Education" 
label var relsd_educ "Std. Dev. Managers' Education"
***label var PT_exports "Exports PT"
label var sh_skilled "Share of Skilled Workers"
label var prgr_spec_exp "Manag. w/ Spec. Exp. (0/1)"
label var empresa "firms"
label var rauch_w "Prod. Diff. Index"
label var r_prgr_spec_exp "Manag. x E * Prod. Diff."
label var mant1 "Other manager (0/1)"
label var mant2 "Sales manager (0/1)"
label var mante1 "OM w/ Export Exp. (0/1)"
label var mante2 "SM w/ Export Exp. (0/1)"

// Fixed Effects
egen prgr_time = group(pr_gr ano)
tab prgr_time,gen(pt_dummy)
egen firm_time = group(empresa ano)
label var firm_time "firm-year pairs"

save MMT_summarystats_tradeb_gr,replace
save MMT_trade_statusb_gr,replace
save MMT_summarystats_trade_intb_gr,replace
save MMT_trade_status_intb_gr,replace

// Locals
local instruct "tex(pretty frag) tdec(3) rdec(3) auto(3) bdec(3) sdec(3) symbol($^a$,$^b$,$^c$) se label e(N) nocons"
local ctr_exp_mf = "relsize relapp_lbprod ln_firmage foreign sh_skilled relmean_age relsd_age relmean_educ relsd_educ"
local ctr_exp_mf_mt = "relsize relapp_lbprod ln_firmage foreign sh_skilled relmean_age relsd_age relmean_educ relsd_educ"

// Regressions sample 
marksample entry_mod2
markout `entry_mod2' rexp* rauch_w r_r3prgr_spec_exp* pr_gr_ pr_gr other_exporter exp prgr_spec_exp r3prgr_spec_exp `ctr_exp_mf' mante1 mante2 empresa prgr_time  // Deflating variables
keep if `entry_mod2'==1
sort empresa ano pr_gr
save MMT_trade_status_entryb_s_gr,replace
save MMT_trade_status_entry_intb_s_gr,replace
keep empresa ano
gen var1 = 1
collapse var1, by(empresa ano) 
save dataset2, replace


//  Creation of Common dataset: here we merge the two parts in order to select the same sample of firms to use in trade status regressions //
 
use dataset1, clear
sort empresa ano
merge 1:1 empresa ano using dataset2
keep if _merge==3
drop _merge
sort empresa ano
drop var1
save common_dataset, replace
